<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { loginByQQ, sendMobileCode, bind } from '@/api/user'
import type { User } from '@/types/user'
import { useUserStore } from '@/stores'
import { showSuccessToast, type FormInstance } from 'vant'
import { useRouter } from 'vue-router'
import { mobileRuels, codeRuels } from '@/utils/rules'
import { useSendMobileCode } from '@/composable'
const store = useUserStore()
const router = useRouter()
const isNeedBind = ref(false)
const mobile = ref('')
const code = ref('')
let _openId = ''
onMounted(() => {
  // QQ js-sdk 提供的方法
  if (window.QC.Login.check()) {
    // 登录成功了
    window.QC.Login.getMe(async openId => {
      _openId = openId
      try {
        const res = await loginByQQ(openId)

        // 之前绑定过，直接后续处理
        loginSuccess(res.data)
      } catch (error) {
        console.log('---error---', error)
        isNeedBind.value = true
      }
    })
  }
})

// 绑定成功了，调用的通用方法
const loginSuccess = (data: User) => {
  // 把用户数据存储到仓库中
  store.setUser(data)

  // 提示
  showSuccessToast('QQ登录成功~')

  // 跳转到 user 页面，使用replace防止用户回退到callback页面
  router.replace('/user')
}

const { form, time, send } = useSendMobileCode(mobile, 'bindMobile')

// 提交
const submit = async () => {
  const res = await bind(mobile.value, code.value, _openId)
  // 绑定成功之后的处理
  loginSuccess(res.data)
}
</script>

<template>
  <div class="login-page" v-if="isNeedBind">
    <cp-nav-bar></cp-nav-bar>
    <div class="login-head">
      <h3>手机绑定</h3>
    </div>
    <van-form autocomplete="off" ref="form" @submit="submit">
      <van-field
        name="mobile"
        v-model="mobile"
        placeholder="请输入手机号"
        :rules="mobileRuels"
      ></van-field>
      <van-field
        name="code"
        v-model="code"
        placeholder="请输入验证码"
        :rules="codeRuels"
      >
        <template #button>
          <span class="btn-send" @click="send" :class="{ active: time > 0 }">{{
            time > 0 ? `${time}秒后重试` : '发送验证码'
          }}</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit">
          立即绑定
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }
  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-icon {
    margin-right: 10px;
  }
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>
